package cn.tedu.ivos.user.service.impl;

import cn.tedu.ivos.base.enums.StatusCode;
import cn.tedu.ivos.base.exception.ServiceException;
import cn.tedu.ivos.user.mapper.UserMapper;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserQueryParam;
import cn.tedu.ivos.user.pojo.dto.UserSavaParam;
import cn.tedu.ivos.user.pojo.entity.User;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 业务层实现类需要添加@Service注解,表示其为业务层实现类
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("用户登录，参数：{}", userLoginParam);
        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
        if (userVO == null) {
            //主业务如果遇到了问题,需要抛出业务层异常,而不是直接给前端返回结果
            //Controller层才是给前端返回结果的那一层
            throw new ServiceException(StatusCode.USERNAME_ERROR.USERNAME_ERROR);
        }
        if (!userVO.getPassword().equals(userLoginParam.getPassword())) {
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        return userVO;
    }

    @Override
    public void saveUser(UserSavaParam userSavaParam) {
        log.debug("保存用户业务：userSaveParam={}", userSavaParam);
        User user = new User();
        BeanUtils.copyProperties(userSavaParam, user);
        //如果没有用户id，说明是新增操作
        if (user.getId() == null) {
            user.setPassword("123456");
            user.setCreateTime(new Date());
            userMapper.insert(user);
        } else {//如果有用户id，说明是编辑更新操作
            //TODO：修改操作
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }
    }

    @Override
    public List<UserVO> selectUserList(UserQueryParam userQuery) {
        log.debug("查询用户业务：userQuery={}", userQuery);
        List<UserVO> list = userMapper.selectUserList(userQuery);
        log.debug("查询结果：list={}", list);
        return list;
    }

    @Override
    public void resetPassword(Long userId) {
        log.debug("重置密码业务：userId={}", userId);
        User user = new User();
        user.setId(userId);
        user.setPassword("123456");
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void updateStatus(Long userId, String status) {
        log.debug("更新用户状态业务：userId={},status={}", userId, status);
        User user = new User();
        user.setId(userId);
        user.setStatus(status);
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void deleteUser(Long userId) {
        log.debug("删除用户业务：userId={}", userId);
        userMapper.deleteById(userId);
    }

}